//
//  HealthDiaryViewController.m
//  hospitalVIP
//
//  Created by Xiaoyan on 12-12-12.
//  Copyright (c) 2012年 MEETStudio. All rights reserved.
//

#import "HealthDiaryViewController.h"

@interface HealthDiaryViewController ()

@end


@implementation HealthDiaryViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)dealloc{
    
    
    [myArray release];
    [myTableView release];
    
    [caseHistoryArray release];
    
    [super dealloc];
    
    
}



- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.title = @"个人健康档案";
    
    // Do any additional setup after loading the view from its nib.
    
    
    if (myArray == nil) {
        myArray = [[NSMutableArray alloc]init];
        
        NSDictionary *dict1 = [[NSDictionary alloc]initWithObjectsAndKeys:@"2012-12-12",@"createDate",@"case_history_1.jpg",@"caseHistoryAttachement",@"这是一个测试用的病历，版权归原作者所有。",@"caseHistoryDesc", nil];
        
        NSDictionary *dict2 = [[NSDictionary alloc]initWithObjectsAndKeys:@"2013-1-3",@"createDate",@"case_history_2.jpg",@"caseHistoryAttachement",@"测试病历二，不做商业盈利用。",@"caseHistoryDesc", nil];
        
        [myArray addObject:dict2];
        [myArray addObject:dict1];
        
        [dict1 release];
        [dict2 release];
        
    }
    
    
    if (caseHistoryArray == nil) {
        caseHistoryArray = [[NSMutableArray alloc]initWithObjects:@"case_history_1.jpg",@"case_history_2.jpg", nil];
       
        
        //图片存入缓存,测试用
            
        if ([[SDDataCache sharedDataCache:kImageCache_Locale_Record]  dataFromKey:@"case_history_1.jpg"] == nil) {
            
            UIImage *image1 = [UIImage imageNamed:@"case_history_1.jpg"];
            UIImage *image2 = [UIImage imageNamed:@"case_history_2.jpg"];
            
            NSData* pictureData1 = [[PublicMethodEngine sharedPublicMethodEngine] compresImageDataWith:image1 maxPix:PHOTO_MAXPIX];
            
            NSString *filePath1 = @"case_history_1.jpg";
            
            [[SDDataCache sharedDataCache:kImageCache_Locale_Record] storeData:pictureData1 forKey:filePath1 ];
            
            
            NSData* pictureData2 = [[PublicMethodEngine sharedPublicMethodEngine] compresImageDataWith:image2 maxPix:PHOTO_MAXPIX];
            
            NSString *filePath2 = @"case_history_2.jpg";
            
            [[SDDataCache sharedDataCache:kImageCache_Locale_Record] storeData:pictureData2 forKey:filePath2 ];

            
        }
        
               
        
    }
    
    
    
    //定义右侧按钮
    UIButton *rightButton1 = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 40, 40)];
    [rightButton1 setImage:[UIImage imageNamed:@"icon_write_iphone.png"] forState:UIControlStateNormal];
    [rightButton1 addTarget:self action:@selector(recordViewAction:) forControlEvents:UIControlEventTouchUpInside];
    
     UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] initWithCustomView:rightButton1];
     self.navigationItem.rightBarButtonItem = rightItem;

    
    
}


- (void)recordViewAction:(id)sender{
    
    RecordViewController *content = [[RecordViewController alloc] init];
    UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:content];
    //CLog(@"content retain count %d", [content retainCount]);
    [content release];
    [self.navigationController presentModalViewController:navController animated:YES];
    
    [navController release];
    

    
}


#pragma mark table dataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    
    return myArray.count;
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    NSInteger section = indexPath.section;
    NSInteger row = indexPath.row;
    
        
    static NSString *MyIdentifier = @"CaseHistoryCell";
	// Try to retrieve from the table view a now-unused cell with the given identifier.
	CaseHistoryCell *cell =(CaseHistoryCell *) [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
	
    if (cell == nil) {
		NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"CaseHistoryCell"
                                                     owner:self options:nil];
#ifdef __IPHONE_2_1
		cell = (CaseHistoryCell *)[nib objectAtIndex:0];
#else
		cell = (CaseHistoryCell *)[nib objectAtIndex:1];
#endif
        
       
        cell.backgroundColor = [UIColor clearColor];
	    cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        UIImageView *bgview = [[UIImageView alloc]initWithFrame:CGRectMake(5, 5, 310,140 )];
        bgview.image = [[UIImage imageNamed:@"writ bg_staggerer_iPhone.png"] stretchableImageWithLeftCapWidth:100 topCapHeight:20];
        [cell.contentView addSubview:bgview];
        [bgview release];

        cell.selectionStyle = UITableViewCellSelectionStyleNone;
              
	}
    
    NSDictionary *dict = [myArray objectAtIndex:row];
    
    [cell loadCellViewWith:dict];
    
       
	return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 360;
    
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    
    [self  toLocalFullSize:caseHistoryArray page:indexPath.row];
    
    
}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


#pragma mark--
#pragma mark 大图查看滑动相册


- (void)toPhotoBook:(NSArray *)images{
    
    DiaryPhotoBrowseViewController *viewController = [[DiaryPhotoBrowseViewController alloc] initWithImages:images  dateArray:nil page:0 localImageNum:0 isPersonalTimeline:YES];
    
    UINavigationController *nav = [[UINavigationController alloc]initWithRootViewController:viewController];
    
   
    [self.navigationController presentModalViewController:nav animated:YES];
    
    [viewController release];
    [nav release];
    
}

//本地大图查看
- (void)toLocalFullSize:(NSArray *)images page:(int)page{
    
    DiaryPhotoBrowseViewController *viewController = [[DiaryPhotoBrowseViewController alloc]initWithLocalImages:images page:page];
    
    UINavigationController *nav = [[UINavigationController alloc]initWithRootViewController:viewController];
    
    
      [self.navigationController presentModalViewController:nav animated:YES];
    
    [viewController release];
    [nav release];
    
    
}



@end
